/*
 * Created on 13-Sep-2006
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package wstrader.task.home.discount;

import java.sql.ResultSet;
import java.util.Vector;
import wstrader.task.trade.*;
import pc.sql.value.BooleanValue;
import pc.sql.value.IntValue;
import pc.system.*;

/**
 * @author NANSA
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class DailyBasis extends TradeTask
{
	  public DailyBasis(Tasks owner)
	  {
	    super(owner);
	  }

	  public long doTask()
	  {
	    String table_name = "grade_discount_allowance_details";
	    int task_id     = this.getRequest().getIntValue("task_id");
	    long focused_id = this.getRequest().getLongValue("id");
	    String uid = this.getRequest().getStringValue("uid");
	     switch (task_id)
		{
	      case 1 : // Update
	      	if(focused_id > 0)
	      	{
	      		focused_id = doUpdate(table_name,focused_id);
	      	}
	        break;
	      case 2 : // Delete
	        focused_id = doDelete(table_name,focused_id);
	        focused_id = 1;
	        break;
	      default : //View only
	        break;
	    }
	    this.setFocusedID(focused_id);

	    return focused_id;
	  }
	  
	  public ResultSet fetchGradeDiscountList()
	  {
	  	
	  	String Sql = "";
	  	int OriginId = getRequest().getIntValue("origin");
	  	int QualityId = getRequest().getIntValue("quality");
	  	int GradeId = getRequest().getIntValue("grade");
	  	
	  	Sql = "SELECT grade_discount_allowance_details.id,grade_discount_allowance_details.grade_master_id, " +
			  "grade_discount_allowance_details.black_discount,grade_discount_allowance_details.black_allowance, " +
			  "grade_discount_allowance_details.broken_discount,grade_discount_allowance_details.broken_allowance, " +
			  "grade_discount_allowance_details.blk_brk_discount,grade_discount_allowance_details.blk_brk_allowance, " +
			  "grade_discount_allowance_details.brown_discount,grade_discount_allowance_details.brown_allowance, " +
			  "grade_discount_allowance_details.excelsa_discount,grade_discount_allowance_details.excelsa_allowance, " +
			  "grade_discount_allowance_details.moldy_discount,grade_discount_allowance_details.moldy_allowance, " +
			  "grade_discount_allowance_details.foreign_matter_discount,grade_discount_allowance_details.foreign_matter_allowance, " +
			  "grade_discount_allowance_details.moisture_discount,grade_discount_allowance_details.moisture_allowance, " +
			  "grade_discount_allowance_details.sc18_discount,grade_discount_allowance_details.sc18_allowance, " +
			  "grade_discount_allowance_details.sc16_discount,grade_discount_allowance_details.sc16_allowance, " +
			  "grade_discount_allowance_details.sc14_discount,grade_discount_allowance_details.sc14_allowance, " +
			  "grade_discount_allowance_details.sc13_discount,grade_discount_allowance_details.sc13_allowance, " +
			  "grade_discount_allowance_details.sc12_discount,grade_discount_allowance_details.sc12_allowance, " +
			  "grade_discount_allowance_details.below_sc12_discount,grade_discount_allowance_details.below_sc12_allowance, " +
			  "grade_master.name,grade_master.quality_id,grade_master.origin_id " +
			  "FROM grade_discount_allowance_details " +
			  "INNER JOIN grade_master ON (grade_discount_allowance_details.grade_master_id=grade_master.id) " +
			  "Where (grade_master.Origin_id = ? OR ?) " +
			  "and (grade_master.Quality_id = ? OR ?) " +
			  "and (grade_master.id = ? OR ?)";
	  	
	    Vector vp = new Vector();

	    vp.addElement(new IntValue(OriginId));
	    vp.addElement(new BooleanValue(OriginId == 0));

	    vp.addElement(new IntValue(QualityId));
	    vp.addElement(new BooleanValue(QualityId == 0));	    
	    
	    vp.addElement(new IntValue(GradeId));
	    vp.addElement(new BooleanValue(GradeId == 0));
	  		
	  	return getSQLCommand().executeQuery(Sql,vp);
	  }	
	  
	  public ResultSet getDiscountGradeList()
	  {
	  	String Sql = "select id,name from grade_master where type = 'P'";
	  	return getSQLCommand().executeQuery(Sql);
	  }
	  
	  public ResultSet getDetails(long id)
	  {
	  	String Sql = "";
	  	Sql = "SELECT grade_discount_allowance_details.id,grade_discount_allowance_details.grade_master_id, " +
		  "grade_discount_allowance_details.black_discount,grade_discount_allowance_details.black_allowance,grade_discount_allowance_details.black_premium, " +
		  "grade_discount_allowance_details.broken_discount,grade_discount_allowance_details.broken_allowance,grade_discount_allowance_details.broken_premium, " +
		  "grade_discount_allowance_details.blk_brk_discount,grade_discount_allowance_details.blk_brk_allowance,grade_discount_allowance_details.blk_brk_premium, " +
		  "grade_discount_allowance_details.brown_discount,grade_discount_allowance_details.brown_allowance,grade_discount_allowance_details.brown_premium, " +
		  "grade_discount_allowance_details.excelsa_discount,grade_discount_allowance_details.excelsa_allowance,grade_discount_allowance_details.excelsa_premium, " +
		  "grade_discount_allowance_details.moldy_discount,grade_discount_allowance_details.moldy_allowance,grade_discount_allowance_details.moldy_premium, " +
		  "grade_discount_allowance_details.foreign_matter_discount,grade_discount_allowance_details.foreign_matter_allowance,grade_discount_allowance_details.foreign_matter_premium, " +
		  "grade_discount_allowance_details.moisture_discount,grade_discount_allowance_details.moisture_allowance,grade_discount_allowance_details.moisture_premium, " +
		  "grade_discount_allowance_details.sc18_discount,grade_discount_allowance_details.sc18_allowance,grade_discount_allowance_details.sc18_premium, " +
		  "grade_discount_allowance_details.sc16_discount,grade_discount_allowance_details.sc16_allowance,grade_discount_allowance_details.sc16_premium, " +
		  "grade_discount_allowance_details.sc14_discount,grade_discount_allowance_details.sc14_allowance,grade_discount_allowance_details.sc14_premium, " +
		  "grade_discount_allowance_details.sc13_discount,grade_discount_allowance_details.sc13_allowance,grade_discount_allowance_details.sc13_premium, " +
		  "grade_discount_allowance_details.sc12_discount,grade_discount_allowance_details.sc12_allowance,grade_discount_allowance_details.sc12_premium, " +
		  "grade_discount_allowance_details.below_sc12_discount,grade_discount_allowance_details.below_sc12_allowance,grade_discount_allowance_details.below_sc12_premium, " +
		  "grade_master.name,grade_master.quality_id,grade_master.origin_id " +
		  "FROM grade_discount_allowance_details " +
		  "INNER JOIN grade_master ON (grade_discount_allowance_details.grade_master_id=grade_master.id) " +
		  "Where (grade_discount_allowance_details.id = " + id +")";
	  	
	  	ResultSet Rs = getSQLCommand().executeQuery(Sql);
	  	return Rs;
	  }
}
